package com.school.graduation.detailed.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.school.graduation.R;
import com.school.graduation.common.base.BaseActivity;
import com.school.graduation.detailed.model.DetailedAdapter;
import com.school.graduation.detailed.presenter.DetailedPresenterNew;
import com.school.graduation.model.Detailed;

import butterknife.Bind;

public class DetailedActivityNew extends BaseActivity implements View.OnClickListener{

    @Bind(R.id.activity_detailed_goodsimg)
    ImageView image;
    @Bind(R.id.activity_detailed_person_head)
    ImageView saleImage;
    @Bind(R.id.activity_detailed_person_name)
    TextView saleName;
    @Bind(R.id.activity_detailed_interaction_headimg)
    ImageView saleImageTwo;
    @Bind(R.id.activity_detailed_babyimg_interaction_recyclerview)
    RecyclerView interaction;
    @Bind(R.id.activity_detailed_price)
    TextView price;
    @Bind(R.id.activity_detailed_title)
    TextView title;
    @Bind(R.id.activity_detailed_content)
    TextView content;
    @Bind(R.id.activity_detailed_livingmessage_layout)
    CardView postLayout;
    @Bind(R.id.activity_detailed_buyNow_new)
    CardView buy;
    private Detailed detailed;
    private MaterialDialog dialog;
    private String prouductId;
    private DetailedPresenterNew presenter;

    public static void jumpToDetail(Context context, Bundle bundle){
        Intent intent = new Intent(context,DetailedActivityNew.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_detail_new;
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        String item_id = intent.getStringExtra("item");
        //ToastUtil.makeShortToast(context,item_id);
        //showDialog();
        presenter = new DetailedPresenterNew(context,this);
        presenter.getDetailed(item_id);
        initRecyclerView();
        initClick();

    }

    public void initRecyclerView(){
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        interaction.setLayoutManager(layoutManager);
    }

    public void initAdapter(DetailedAdapter adapter){
        interaction.setAdapter(adapter);
    }

    public void initImage(String url){
        if (url != null && !url.isEmpty()){
            Glide.with(context)
                    .load(url)
                    .into(image);
        }
    }


    public void initContent(final Detailed detailed){
        //title.setText(detailed.getProduct().getCatogaryName());
        price.setText(detailed.getProduct().getPrice()+"");
        content.setText(detailed.getProduct().getDetail());
        saleName.setText(detailed.getProduct().getSellerName());
        prouductId = detailed.getProduct().get_id();
        //this.detailed = detailed;
        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,BuyActivity.class);
                intent.putExtra("data",detailed);
                startActivity(intent);
            }
        });
    }

    private void initClick(){
        postLayout.setOnClickListener(this);
    }

    private void showDialog(){
        dialog = new MaterialDialog.Builder(context)
                .title("请稍等")
                .progress(true,0)
                .show();
    }

    private void dissmissDialog(){
        dialog.dismiss();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 2){
            Detailed detailed = (Detailed)data.getSerializableExtra("data");
            if (detailed != null){
                presenter.upData(detailed);
            }
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()){
            case R.id.activity_detailed_livingmessage_layout:
                intent = new Intent(context,DialogBoxActivityNew.class);
                intent.putExtra("product",prouductId);
                break;
            /*case R.id.activity_detailed_buyNow:
                intent = new Intent(context,BuyActivity.class);
                intent.putExtra("data",detailed);
                break;*/
        }
        startActivityForResult(intent,1);
    }
}
